﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;

using System.Web.UI.HtmlControls;

namespace GMS
{
    public partial class Pages_SelectOptions : System.Web.UI.Page
    {
        static Control msg = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                rblProjectType.DataSource = Common.GetProjectType();
                rblProjectType.DataBind();

                rblProjUrgency.DataSource = Common.GetProjectUrgency();
                rblProjUrgency.DataBind();

                // when workflow gets back to this page - display the values which are entered previously
                // these two should remember the position not the value - for now; 
                rblProjectType.SelectedIndex = SessionVariable.Instance.UserDetails.Options.ProjectTypeId - 1;
                rblProjUrgency.SelectedIndex = SessionVariable.Instance.UserDetails.Options.ProjectUrgencyId - 1;

                txtDesc.Text = string.IsNullOrEmpty(SessionVariable.Instance.UserDetails.Options.Description) ? string.Empty : SessionVariable.Instance.UserDetails.Options.Description;
                txtCIName.Text = string.IsNullOrEmpty(SessionVariable.Instance.UserDetails.Name) ? string.Empty : SessionVariable.Instance.UserDetails.Name;
                txtEmail.Text = SessionVariable.Instance.UserDetails.Email;
                txtPhone.Text = string.IsNullOrEmpty(SessionVariable.Instance.UserDetails.PhoneNumber) ? string.Empty : SessionVariable.Instance.UserDetails.PhoneNumber.ToString();
                txtZip.Text = string.IsNullOrEmpty(SessionVariable.Instance.UserDetails.ZipCode.ToString()) ? string.Empty : SessionVariable.Instance.UserDetails.ZipCode.ToString();

                if (txtZip.Text == "0")
                {
                    txtZip.Text = string.Empty;
                }
            }
            else
            {
                if (msg != null)
                {
                    msg.Visible = false;   
                }
            }
        }

        private void PersistUserDataInSession()
        {
            try
            {
                SessionVariable.Instance.UserDetails.Name = txtCIName.Text;
                SessionVariable.Instance.UserDetails.PhoneNumber = txtPhone.Text;
                SessionVariable.Instance.UserDetails.Email = txtEmail.Text;
                SessionVariable.Instance.UserDetails.Options.Description = string.IsNullOrEmpty(txtDesc.Text) ? string.Empty : txtDesc.Text;

                SessionVariable.Instance.UserDetails.Options.ProjectTypeId = Convert.ToInt32(rblProjectType.SelectedValue);
                SessionVariable.Instance.UserDetails.Options.ProjectUrgencyId = Convert.ToInt32(rblProjUrgency.SelectedValue);
                SessionVariable.Instance.UserDetails.ZipCode = Convert.ToInt32(txtZip.Text);
                SessionVariable.Instance.UserDetails.IsContactedByPhone = false; //descoped on 11/18/11 rblCallBack.SelectedValue == "Yes" ? true : false;

                SessionVariable.Instance.UserDetails.Cost.ZipDistance = Convert.ToInt32(ZipCalculator.GetDistance(SessionVariable.Instance.UserDetails.ZipCode));
                SessionVariable.Instance.UserDetails.Cost.ZipSurcharge = Common.GetZipSurchargeByDistance(SessionVariable.Instance.UserDetails.Cost.ZipDistance);
            }
            catch (System.Exception e)
            {
                ShowMessage("Please fill in all the details - Project Type, Project Urgency, Name, Email, Phone Number, Zip Code ");
            }
        }

        private void CalculateTotalPrice()
        {
            double total = 0;

            SessionVariable.Instance.UserDetails.Cost.BacksplashPrice = SessionVariable.Instance.UserDetails.Cost.SurfacePrice;
           
            total = Utility.CalculateTotalCostWithOutTax(SessionVariable.Instance.UserDetails.Cost.Backsplash,
                SessionVariable.Instance.UserDetails.Cost.BacksplashPrice,
                SessionVariable.Instance.UserDetails.Cost.Edge,
                SessionVariable.Instance.UserDetails.Cost.EdgePrice,
                SessionVariable.Instance.UserDetails.Cost.Sinkout,
                SessionVariable.Instance.UserDetails.Cost.SinkoutPrice,
                SessionVariable.Instance.UserDetails.Cost.Surface,
                SessionVariable.Instance.UserDetails.Cost.SurfacePrice,
                SessionVariable.Instance.UserDetails.Cost.ZipDistance,
                SessionVariable.Instance.UserDetails.Cost.ZipSurcharge);

            total = Utility.CalculateTotalCostWithTax(total, SessionVariable.Instance.UserDetails.Cost.Tax);
            SessionVariable.Instance.UserDetails.Cost.TotalPrice = Convert.ToInt32(total);
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("SinkOptions.aspx");
        }

        protected void btnFinish_Click(object sender, EventArgs e)
        {
            PersistUserDataInSession();
            try
            {
                if (!string.IsNullOrEmpty(SessionVariable.Instance.UserDetails.Name) &&
                    !string.IsNullOrEmpty(SessionVariable.Instance.UserDetails.Email) &&
                    !string.IsNullOrEmpty(SessionVariable.Instance.UserDetails.PhoneNumber) &&
                    SessionVariable.Instance.UserDetails.ZipCode > 0 &&
                    SessionVariable.Instance.UserDetails.Shape != null &&
                    SessionVariable.Instance.UserDetails.Options != null &&
                    SessionVariable.Instance.UserDetails.Cost != null)
                {
                    CalculateTotalPrice();

                    int result = Common.AddUserDetails(SessionVariable.Instance.UserDetails.Name,
                        SessionVariable.Instance.UserDetails.Email,
                        SessionVariable.Instance.UserDetails.PhoneNumber,
                        SessionVariable.Instance.UserDetails.ZipCode,
                        SessionVariable.Instance.UserDetails.IsContactedByPhone,
                        SessionVariable.Instance.UserDetails.Shape,
                        SessionVariable.Instance.UserDetails.Options,
                        SessionVariable.Instance.UserDetails.Cost);

                    if (result > 0)
                    {
                        SessionVariable.Instance.UserDetails.UserId = result;
                        SessionVariable.Instance.UserDetails.IsDataPersisted = true;
                        Response.Redirect("Estimate.aspx");
                    }

                }
                else
                {
                    ShowMessage("Please fill in all the details - Project Type, Project Urgency, Name, Email, Phone Number, Zip Code ");
                }

            }
            catch (System.Exception ex)
            {
                ShowMessage("Please fill in all the details - Project Type, Project Urgency, Name, Email, Phone Number, Zip Code ");
            }
        }

        private void ShowMessage(string msgText)
        {
            HtmlGenericControl divMsg = (HtmlGenericControl)Page.Master.FindControl("divMsg");

            if (divMsg != null)
            {
                msg = this.Page.LoadControl("Controls/Message.ascx");

                if (msg != null)
                {
                    Label lblMessage = (Label)msg.FindControl("lblMessage");

                    if (lblMessage != null)
                    {
                        lblMessage.Text = msgText;
                    }

                    divMsg.Controls.Add(msg);
                }
            }

        }

    }
}